import java.util.Arrays;

//**************************数组*********************************
public class Test {

    public static void main1(String[] args) {
        int[] array1 = new int[10];//申请10个整型空间 --- 初始化默认为0
        int[] array2 = new int[]{1,2,3,4,5,6,7,8,9,10};//申请10个整型空间并初始化为1-10

        boolean[] array3 = new boolean[10];//申请10个boolean空间 --- 初始化默认为false

        String[] array4 = new String[]{"hello", "Java", "!"};

        //打印array1
        System.out.print("array1 = ");
        for(int i = 0; i < array1.length; i++) {
            System.out.print(array1[i] + " ");
        }
        System.out.println();

        //打印array2
        System.out.print("array2 = ");
        for(int i = 0; i < array2.length; i++) {
            System.out.print(array2[i] + " ");
        }
        System.out.println();

        //打印array3
        System.out.print("array3 = ");
        for(int i = 0; i < array3.length; i++) {
            System.out.print(array3[i] + " ");
        }
        System.out.println();

        //打印array4
        System.out.print("array4 = ");
        for(int i = 0; i < array4.length; i++) {
            System.out.print(array4[i] + " ");
        }
        System.out.println();
    }



    public static void main2(String[] args) {

        int[] array1 = new int[]{1,2,3,4,5};
        array1[0] = 100;
        array1[1] = 200;

        int[] array2 = new int[]{1,2,3,4,5};

        array2 = array1;//array2引用指向的array1引用指向的对象了

        System.out.print("array1 = ");
        for(int i = 0; i < array1.length; i++) {
            System.out.print(array1[i] + " ");
        }
        System.out.println();

        System.out.print("array2 = ");
        for(int i = 0; i < array2.length; i++) {
            System.out.print(array2[i] + " ");
        }
        System.out.println();
    }

    //for-each打印数组
    public static void main3(String[] args) {
        int[] array = new int[]{1,2,3,4,5,6,7};

        for(int x : array) {
            System.out.print(x + " ");
        }
        System.out.println();
    }



    public static void print(int[] array) {
        for(int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
        System.out.println();
    }

    public static void main4(String[] args) {

        int[] array = new int[]{1,2,3,4,5,6,7,8};
        print(array);
    }


//数组地址的修改 -- 引用指向对象的修改
    public static void main5(String[] args) {
        int[] array1 = new int[3];
        array1[0] = 1;
        array1[1] = 2;
        array1[2] = 3;

        int[] array2 = new int[]{1,2,3,4,5};
        array2[0] = 100;
        array2[1] = 200;

        array1 = array2;//array1的引用指向array2引用指向的对象，那么array1没人引用指向，那么array1为null
        array1[2] = 200;
        array1[3] = 300;
        array1[4] = 400;

        System.out.print("array2 = ");
        for(int i = 0; i < array2.length; i++) {
            System.out.print(array2[i] + " ");
        }
        System.out.println();

    }

    public static void change(int[] array) {
        array[0] = 10086;
    }
    public static void main6(String[] args) {
        int[] array = new int[]{1,2,3,5};

        change(array);

        System.out.print("array = ");
        for(int x : array) {
            System.out.print(x + " ");
        }
        System.out.println();
    }



    //数组作为返回值传递 --- 注意返回值
    public static int[] fun(){
        int[] array = new int[]{1,2,3,4,5,6,7,8,9,10};
        return array;
    }

    public static void main7(String[] args) {
        int[] array1 = fun();

        System.out.print("array1 = ");
        for(int x : array1) {
            System.out.print(x + " ");
        }
        System.out.println();
    }


    //Arrays的使用
    public static void main8(String[] args) {
        int[] array1 = new int[]{1,2,3,4,5};

        System.out.print("array1 = ");
        String ret = Arrays.toString(array1);
        System.out.println(ret);



        double[] array2 = new double[]{1.1,2.1,3.1,4.1,5.1};

        System.out.print("array2 = ");
        ret = Arrays.toString(array2);
        System.out.println(ret);


        String[] array3 = new String[]{"hello", "Java", "!!"};
        ret = Arrays.toString(array3);
        System.out.println(ret);

    }



    //实现一个方法 transform, 以数组为参数, 循环将数组中的每个元素 乘以 2 , 并设置到对应的数组元素上. 例如 原数组为 {1, 2, 3}, 修改之后为 {2, 4, 6}
    public static void transform(int[] array) {
        for(int i = 0; i < array.length; i++) {
            array[i] *= 2;
        }
    }

    public static void main9(String[] args) {
        int[] array = new int[]{1,2,3};
        transform(array);
        String ret = Arrays.toString(array);
        System.out.println(ret);
    }



    //调整数组顺序使得奇数位于偶数之前。调整之后，不关心大小顺序。
    //如数组：[1,2,3,4,5,6]
    //调整后可能是：[1, 5, 3, 4, 2, 6]
    public static void main10(String[] args) {
        int[] array = new int[]{6,2,1,3,5,7,9};

        int i = 0;
        int j = array.length - 1;
        while(i < j) {
            if(array[i] % 2 == 0) { //因为偶数要在后面，奇数要在前面
                int tmp = array[i];
                array[i] = array[j];
                array[j] = tmp;
            }
            i++;
            j--;
        }

        String ret = Arrays.toString(array);
        System.out.println(ret);
    }





    //给定一个非空整数数组，除了某个元素只出现一次以外，其余每个元素均出现两次。找出那个只出现了一次的元素。
    public static void main11(String[] args) {

        int[] array = new int[]{4,5,3,4,7,3,7,5,1};

        int ret = 0;

        for(int i = 0; i < array.length; i++) {
            ret ^= array[i];
        }
        System.out.println(ret);

    }




    //输入：nums = [2,7,11,15], target = 9
    //输出：[0,1]
    //解释：因为 nums[0] + nums[1] == 9 ，返回 [0, 1]
    public static int[] sumArray(int[] array, int target) {

        int[] tmp = new int[]{-1,-1};

        for(int i = 0; i < array.length; i++) {
            for(int j = i + 1; j < array.length; j++) {
                if(array[i] + array[j] == target) {
                    tmp[0] = i;
                    tmp[1] = j;
                }
            }
        }
        return tmp;
    }

    public static void main12(String[] args) {
        int[] array = new int[]{2,7,11,15};

        int[] ret = sumArray(array, 9);
        System.out.println(Arrays.toString(ret));

    }




    //给你一个整数数组 arr，请你判断数组中是否存在连续三个元素都是奇数的情况：如果存在，请返回 true ；否则，返回 false 。
    public static boolean first(int[] array) {
        int count = 0;//计数器，

        for(int i = 0; i < array.length; i++) {
            if(array[i] % 2 != 0) {
                count++;
                if(count == 3) {
                    return true;
                }
            } else {
                count = 0;
            }
        }
        return false;
    }

    public static void main13(String[] args) {
        int[] array = new int[]{3,2,8,3,5,7,10};
        boolean ret = first(array);
        System.out.println(ret);
    }





    //给定一个大小为 n 的数组，找到其中的多数元素。多数元素是指在数组中出现次数 大于 ⌊ n/2 ⌋ 的元素
    //多数元素一定是在array数组的中间mid，所以我们直接计算mid即可
    public static int count(int[] array) {
        //先排序
        Arrays.sort(array);
        return array[array.length / 2];
    }

    public static void main14(String[] args) {
        int[] array = new int[]{2,2,1,1,5,1,2,2,3};

        int ret = count(array);
        System.out.println(ret);
    }





    //数组的逆序
    public static void swap(int[] array, int i, int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }

    public static void reverse(int[] array) {
        int i = 0;
        int j = array.length - 1;
        while(i < j) {
            swap(array, i, j);
            i++;
            j--;
        }
    }

    public static void main15(String[] args) {
        int[] array = new int[]{1,2,3,4,5,6,7,8,9,10};
        reverse(array);
        String ret = Arrays.toString(array);
        System.out.println(ret);
    }





    //给定一个整型数组, 实现冒泡排序(升序排序)
    public static void bubbleSort(int[] array) {


        for(int i = 0; i < array.length - 1; i++) {  //趟数
            boolean flag = true;

            for(int j = 0; j < array.length - 1 - i; j++) {
                if(array[j] > array[j + 1]) {
                    flag = false;
                    int tmp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = tmp;
                }
            }
            if(flag) {
                break;
            }
        }
    }

    public static void main16(String[] args) {
        //int[] array = new int[]{5,6,4,1,2,3,8,9,10,0,7};
        int[] array = new int[]{1,2,3,4,5,6,7,8,9,10};
        bubbleSort(array);
        String ret = Arrays.toString(array);
        System.out.println(ret);
    }





    //二分查找 --- 给定一个target值，如果找到了返回target下标，否则返回-1
    //二分查找的前提 --- 必须是一个有序的数组
    public static int binarySearch(int[] array, int target) {
        int left = 0;
        int right = array.length - 1;
        while(left <= right) {

            int mid = (left + right) / 2;

            if(array[mid] < target) {
                left = mid + 1;
            } else if(array[mid] == target) {
                return mid;
            } else {
                right = mid - 1;
            }
        }
        return -1;
    }
    public static void main17(String[] args) {
        int[] array = new int[]{1,2,3,4,5,6,7,8,9,10};

        int ret = binarySearch(array, 8);
        System.out.println(ret);


        //也可以使用Arrays.binarySearch
        int[] array1 = new int[]{3,5,2,1,6,7,9,4};

        Arrays.sort(array1);//二分查找的前提是在有序的数组

        int n = Arrays.binarySearch(array1,4);

        System.out.println(n);


    }



    //数组的拷贝 --- 第一种方法
    public static void main18(String[] args) {
        int[] array1 = new int[]{1,2,3,4,5,6,7,8,9,10};

        int [] array2 = new int[array1.length];

        for(int i = 0; i < array1.length; i++) {
            array2[i] = array1[i];
        }

        String ret1 = Arrays.toString(array1);
        String ret2 = Arrays.toString(array2);

        System.out.print("array1 = ");
        System.out.println(ret1);

        System.out.print("array2 = ");
        System.out.println(ret2);
    }


    //数组拷贝 --- 第二种方法
    public static void main19(String[] args) {
        int[] array1 = new int[]{1,2,3,4,5,6,7,8,9,10};

        int[] array2 = Arrays.copyOf(array1,array1.length);

        String ret1 = Arrays.toString(array1);
        String ret2 = Arrays.toString(array2);

        System.out.print("array1 = ");
        System.out.println(ret1);

        System.out.print("array2 = ");
        System.out.println(ret2);


        //也可以拷贝指定区间
        int[] array3 = Arrays.copyOfRange(array1,2,6);//注意，这里的区间是[2,6)
        String ret3 = Arrays.toString(array3);

        System.out.print("array3 = ");
        System.out.println(ret3);

    }



    //Java中也有提供排序的方法
    public static void main20(String[] args) {
        int[] array = new int[]{4,5,3,6,2,1,8,9,5,10,7};

        Arrays.sort(array);

        String ret = Arrays.toString(array);
        System.out.println(ret);
    }




    //二维数组
    public static void main21(String[] args) {
        //第一种方法
        int[][] array1 = new int[][]{{1,2,3,4,5},{6,7,8,9,10}};

        //第二种方法
        int[][] array2 = {{1,2,3,4,5},{6,7,8,9,10}};

        //第三种方法
        int[][] array3 = new int[2][];//行不可以省略，但是列可以省略

        //打印
        System.out.println("array1 :");
        for(int i = 0; i < array1.length; i++) {
            for(int j = 0; j < array1[i].length; j++) {
                System.out.print(array1[i][j] + " ");
            }
            System.out.println();
        }

        System.out.println("array2 :");
        for(int i = 0; i < array1.length; i++) {
            for(int j = 0; j < array1[i].length; j++) {
                System.out.print(array1[i][j] + " ");
            }
            System.out.println();
        }

        System.out.println("array3 :");
        for(int i = 0; i < array1.length; i++) {
            for(int j = 0; j < array1[i].length; j++) {
                System.out.print(array1[i][j] + " ");
            }
            System.out.println();
        }
    }


    public static void main(String[] args) {
        int[][] array = new int[3][];//确定了行数为3，但是没有确定列

        array[0] = new int[3];//意思是第1行有3列
        array[1] = new int[5];//意思是第2行有5列
        array[2] = new int[2];//意思是第3行有2列

        //在打印一位数组的时候我们可以使用Arrays.toString
        //而我们在打印二维数组的时候需要使用deepToString

        String ret = Arrays.deepToString(array);
        System.out.println(ret);
    }

}
